package com.daiqee.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.daiqee.controller.support.BaseController;
import com.daiqee.pojo.ProductionStatistics;
import com.daiqee.service.impl.ProductionStatisticsService;
import com.daiqee.util.MyBatisPlusUtils;
import com.daiqee.util.WarpperStringUtils;

/**
 * 生产情况统计
 * 
 * <br/>
 * @since JDK 1.8
 * @version  V2.4
 * @author RichardTang 
 * @date: 2018年6月7日
 * @package com.daiqee.controller
 * @copyright:Copyright (c) 2018, 1245811923@qq.com All Rights Reserved.
 */

@Controller
@RequestMapping("production/pro-statics")
public class ProductionStatisticsController extends BaseController{

	@Autowired
	private ProductionStatisticsService productService;
	
	/**
	 * 查询 货物信息
	 */
	@RequestMapping(method=RequestMethod.GET)
	public ResponseEntity<List<ProductionStatistics>> query(ProductionStatistics product,String dateScope){
		
		try {
			
			EntityWrapper<ProductionStatistics> ew = 
			    MyBatisPlusUtils.getTimeScopeCondition(product, dateScope, "datetime");
			
			List<ProductionStatistics> products = productService.selectList(ew);
			
			if (null != products && !(products.isEmpty())) {
				return ResponseEntity.ok(products);
			}
			
		}catch (Exception e) {
			logger.error(e.getMessage());
			return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
		}
		return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
	}
	
	/**
	 * 添加 货物
	 */
	@RequestMapping(method=RequestMethod.POST)
	public ResponseEntity<Void> add(ProductionStatistics product){
		
		try {
			
			isNormalFinish = productService.insert(product);
			
			if (isNormalFinish) {
				return ResponseEntity.status(HttpStatus.CREATED).build();
			}
			
		}catch (Exception e) {
			
			logger.error(e.getMessage());
			
			return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
			
		}
		
		return ResponseEntity.badRequest().build();
	}
	
	/**
	 * 删除 货物
	 */
	@RequestMapping(value="/{ids}",method=RequestMethod.DELETE)
	public ResponseEntity<Void> remove(@PathVariable("ids") String ids){
		try {
			
			List<String> idList = WarpperStringUtils.getIdListByCommaSplit(ids);
			
			isNormalFinish = productService.deleteBatchIds(idList);
			
			if(isNormalFinish) {
				return ResponseEntity.ok().build();
			}
			
		}catch (Exception e) {
			return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
		}
		return ResponseEntity.notFound().build();
	}
	
	/**
	 * 更新 入库信息
	 */
	@RequestMapping(method=RequestMethod.PUT)
	public ResponseEntity<Void> update(ProductionStatistics product){
		
		try {
			
			isNormalFinish =  productService.updateById(product);
			
			if (isNormalFinish) {
				return ResponseEntity.status(HttpStatus.CREATED).build();
			}
			
		}catch (Exception e) {
			
			logger.error(e.getMessage());
			
			return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
		}
		
		return ResponseEntity.badRequest().build();
	}
}
